package controller

import (
	"fmt"
	"strconv"
	"uap/common"
	"uap/pkg/app"
	"uap/pkg/logger"
	"uap/utils"

	"github.com/gin-gonic/gin"
)

type TbUser struct {
	Id         int    `gorm:"column:id;primary_key;NOT NULL" json:"id"`
	Username   string `gorm:"column:username;unique;NOT NULL" json:"username"`
	Password   string `gorm:"column:password" json:"password"`
	Nickname   string `gorm:"column:nickname" json:"nickname"`
	CreateDate string `gorm:"create_date" json:"createDate"`
}

func NewUser() *TbUser {
	return &TbUser{}
}

type LoginRequest struct {
	Username string `json:"username"`
	Password string `json:"password"`
	Captcha  string `json:"captcha"`
	Uuid     string `json:"uuid"`
}

func (a *TbUser) Login(ctx *gin.Context) {
	var loginRequest LoginRequest
	err := ctx.ShouldBind(&loginRequest)
	if err != nil {
		fmt.Println(err)
	}

	code, ok := common.LocalCache.Get(loginRequest.Uuid)
	if !ok {
		fmt.Println("")
	}

	if code.(string) != loginRequest.Captcha {
		app.Error(ctx, app.CodeError.Code, app.CodeError.Msg)
		return
	}

	var tbuser TbUser
	common.Db.Table("tb_user").Where("username = ?", loginRequest.Username).First(&tbuser)
	if tbuser.Password == "" {
		app.Error(ctx, app.UserNoExist.Code, app.UserNoExist.Msg)
		return
	}
	if tbuser.Password == loginRequest.Password {
		user := utils.User{Username: tbuser.Username, Nickname: tbuser.Nickname, Id: strconv.Itoa(tbuser.Id)}
		logger.Infof("login user:%v", user)
		token, err3 := utils.GenToken(user)
		if err3 != nil {
			fmt.Println("生成token异常", err3)
		}
		app.Success(ctx, token)
		return
	} else {
		app.Error(ctx, app.UserOrPasswordError.Code, app.UserOrPasswordError.Msg)
		return
	}
}

func (a *TbUser) Logout(ctx *gin.Context) {

}

func (a *TbUser) Add(c *gin.Context) {
	var user TbUser
	err := c.ShouldBind(&user)
	if err != nil {
		fmt.Println(err)
	}
	common.Db.Table("tb_user").Create(&user)
	app.Success(c, "")
}

func (a *TbUser) List(c *gin.Context) {
	var list []TbUser
	common.Db.Table("tb_user").Scan(&list)
	app.Success(c, list)
}

func (a *TbUser) Delete(c *gin.Context) {
	id := c.DefaultQuery("id", "")
	sql := "delete from tb_users where id = ?"
	common.Db.Table("tb_user").Exec(sql, id)
	app.Success(c, "")
}

func (a *TbUser) Update(c *gin.Context) {
	var user TbUser
	err := c.ShouldBind(&user)
	if err != nil {
		fmt.Println(err)
	}
	common.Db.Table("tb_user").Updates(user)
	app.Success(c, "")
}

func (a *TbUser) GetById(c *gin.Context) {
	id := c.DefaultQuery("id", "")
	var user TbUser
	common.Db.Table("tb_user").Where("id = ?", id).First(&user)
	app.Success(c, user)
}

func (a *TbUser) GetUserInfo(c *gin.Context) {
	token := c.DefaultQuery("token", "")
	user, err := utils.GetTokenUser(token)
	logger.Infof("user:%v", user)
	if err != nil {
		fmt.Println(err)
		app.Error(c, app.UserNoExist.Code, app.UserNoExist.Msg)
		return
	}
	app.Success(c, user)
}

func (a *TbUser) GetCurrentUser(c *gin.Context) {
	token := c.DefaultQuery("token", "")
	if token == "" {
		app.Error(c, 300, "")
		return
	}
	user, error := utils.GetTokenUser(token)
	if error != nil {
		app.Error(c, 300, "")
		return
	}
	app.Success(c, user)
}
